<!--
This example demonstrates the minimum amount of code required to use Mergely.
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <title>Mergely - Simple Example</title>
    <meta http-equiv="X-UA-Compatible" content="chrome=1, IE=edge">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <meta name="description" content="Merge and Diff your documents with diff online and share"/>
    <meta name="keywords" content="diff,merge,compare,jsdiff,comparison,difference,file,text,unix,patch,algorithm,saas,longest common subsequence"/>
    <meta name="author" content="Jamie Peabody"/>

    <!-- Requires jQuery -->
    <script type="text/javascript" src="../../jquery.min.js"></script>

    <!-- Requires CodeMirror -->
    <script type="text/javascript" src="../lib/codemirror_5.32.0.min.js"></script>
    <script type="text/javascript" src="../lib/searchcursor.min.js"></script>
    <link type="text/css" rel="stylesheet" href="../lib/codemirror_5.32.0.min.css"/>

    <!-- Requires Mergely -->
    <script type="text/javascript" src="../lib/mergely.js"></script>
    <script type="text/javascript" src="../../gzip/pako.min.js"></script>
    <script type="text/javascript" src="../../gzip/base64_gzip.js"></script>

    <script type="text/javascript" src="../../sql/codemirror.js"></script>
    <script type="text/javascript" src="../../sql/matchbrackets.js"></script>
    <script type="text/javascript" src="../../sql/javascript.js"></script>
<!--    <script type="text/javascript" src="../../sql/show-hint.js"></script>-->

    <link type="text/css" rel="stylesheet" href="../lib/mergely.css"/>
<!--    <link rel="stylesheet" href="../../../../css/plugin/sql/codemirror.css" />-->
<!--    <link rel="stylesheet" href="../../../../css/plugin/sql/show-hint.css" />-->
    <link rel="stylesheet" href="../../../../css/bootstrap_sybn_custom.css" />
    <style>
    .script_div > .CodeMirror{min-height:90px;height:calc(100% - 2.5em);border: 1px solid #ccc;}
    .mergely_div{height:80vh; min-height:300px; }
    .script_div{height:20vh; min-height:120px;}
    </style>
    <title>sql翻译工具</title>
    <script type="text/javascript">
		$(document).ready(function () {
			// https://www.mergely.com/doc
			$('#mergely').mergely({
				license: 'lgpl-separate-notice',
				ignorews: true, // 是否忽略空白
				ignorecase: true, // 是否忽略大小写
				cmsettings: {
					readOnly: false // 是否只读
				},
				lhs: function(setValue) {
					var lZip=getQueryString('lzip');
					var lText=getQueryString('l','');
					if (lZip) {
						lText=unzip(lZip);
					}
                    if (!lText){
                        lText = "select \r\n date_format(a.create_time, '%Y-%m-%d %H:%i:%s')," +
                            "\r\n STR_TO_DATE(a.tag_create_date, '%Y-%m-%d %H:%i:%s')" +
                            "\r\n group_concat(distinct xxx)" +
                            "\r\n from xxx \r\n where a GLOBAL in (1,2) \r\n LIMIT 1, 2";
                    }
					setValue(lText);
				},
				rhs: function(setValue) {
					var rZip=getQueryString('rzip');
					var rText=getQueryString('r','');
					if (rZip) {
						rText=unzip(rZip);
					}
					setValue(rText);
				}
			});

			window["javascript_editor"] = CodeMirror.fromTextArea($('#script')[0], {
                mode: 'text/javascript', // sybn 自定义sql高亮
                indentWithTabs: true,
                smartIndent: true,
                lineNumbers: true,
                matchBrackets : true,
                lineWrapping: true, //是否自动换行
                autofocus: true,
                viewportMargin: 100,
                autoRefresh: true
            });

		});

/**
 * 获取指定请求参数.
 *
 * @param name 参数名 必填
 * @param defaults 默认值, 可选
 * @param url 可选, 默认为当前页url
 */
function getQueryString(name, defaults, url) {
  var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(&|$)", "i");
  var r = (url) ? url.match(reg) : window.location.search.substr(1).match(reg);
  if (r != null) return decodeURIComponent(r[2]);
  return typeof defaults != undefined ? defaults : null;
}
var historyList = [];
function repl() {
    var input = $("#mergely").mergely('get', 'rhs');
    if (!input) {
     input = $("#mergely").mergely('get', 'lhs');
    }
    const script1 = window["javascript_editor"].getValue() || document.getElementById('script').value;
    var script2 = 'var s = input;s=' + script1;
    var newText = eval(script2);
    if (newText == input) {
        document.getElementById('prompt').innerText = "没有变化";
    } else if (newText) {
        $("#mergely").mergely('rhs', newText);
        historyList.push(input);
        document.getElementById('prompt').innerText = "完成! 大小:" + input.length + " => " + newText.length;
    } else {
        document.getElementById('prompt').innerText = "失败";
    }
}
function newPage() {
	var lText = $("#mergely").mergely('get', 'lhs');
	var rText = $("#mergely").mergely('get', 'rhs');
	var lzip = zip(lText);
	var rzip = zip(rText);
	window.open("simple.html?lzip="+lzip+"&rzip="+rzip);
}

    </script>
</head>
<body>

<div class="mergely-full-screen-8">
    <div class="mergely-resizer">
        <div class="script_div">
			<textarea id="script" rows="4" style="width:99%;">
s=s.replace(/as varchar/gi, "as String");
s=s.replace(/bi_(dm|dw|ods)_[a-z_]+./g, "");
s=s.replace(/((FULL *)?[a-zA-Z]* +JOIN)/gi, "GLOBAL $1");
s=s.replace(/( +in +\( *select)/gi, " GLOBAL$1");
s=s.replace(/array_agg\(DISTINCT /gi, "groupUniqArray(");
s=s.replace(/array_join/g, "arrayStringConcat");
s=s.replace(/array_agg/g, "groupArray");;
s=s.replace(/date_diff\(/gi, "dateDiff(");
s=s.replace(/curdate\(\)/gi, "today()");
s=s.replace(/cast\(substring\(([a-zA-Z0-9._]+),1,10\) as date\)/gi, "cast($1 as Nullable(Date))");
s=s.replace(/date_format\(/g,'formatDateTime(');
s=s.replace(/IF\(/g,'if(');
s=s.replace(/SUM\(/g,'sum(');
s=s.replace(/date_parse\(([a-zA-Z0-9@${}:]+) *, +[\'\"]%Y-%m-%d[\'\"]\)/g,'toDate($1)');
s=s.replace(/date_add\('day', +(-?[0-9]+), +toDate\(([a-zA-Z0-9@${}:]+)\)\)/g,'addDays(toDate($2),$1)');
s=s.replace(/date_add\('day', +(-?[0-9]+), +([a-zA-Z0-9@${}:]+)\)/g,'addDays($2,$1)');
s=s.replace(/(dim_mer_mer_dadi|dim_mer_sku_dadi) where tenant_id='321566' and date_key = '([a-zA-Z0-9@${}:_]+)'/g,"$1 where tenant_id='321566'");
s=s.replace(/date_key *= *[\'\"]\$\${date_key_([a-zA-Z0-9_]+)}[\'\"]/g,"date_key GLOBAL in (select max(date_key) from $1 where date_key between formatDateTime(addDays(now(), -7), '%Y-%m-%d') and formatDateTime(now(), '%Y-%m-%d'))");
			</textarea>
            <button onclick="repl()">转换</button>
            <span>结果:</span> <span id="prompt"></span> <a href="###" onclick="newPage()">新窗口打开</a>
        </div>
        <div class="mergely_div">
            <div id="mergely">
            </div>
        </div>
    </div>
</div>
</body>
</html>
